package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.worldcup.*;
import com.yanqu.road.pb.activity.WorldCupProto;
import com.yanqu.road.server.manger.ServerLanguageMgr;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class WorldCupActivityPb {

    public static WorldCupProto.WorldCupConfigSyncMsg.Builder builderSyncConfigMsg(WorldCupConfig config, String language) {
        WorldCupProto.WorldCupConfigSyncMsg.Builder builder = WorldCupProto.WorldCupConfigSyncMsg.newBuilder();
        for (WorldCupPlayerConfig ballerConfig : config.getBallerConfig().values()) {
            WorldCupProto.WorldCupBallerConfigTemp.Builder ballerConfigBuilder = WorldCupProto.WorldCupBallerConfigTemp.newBuilder();
            ballerConfigBuilder.setQuality(ballerConfig.getQuality());
            ballerConfigBuilder.setIcon(ballerConfig.getIcon());
            String toName = ServerLanguageMgr.getContent(ballerConfig.getName(), language);
            ballerConfigBuilder.setName(toName);
            ballerConfigBuilder.setExpAdd(ballerConfig.getExpAdd());
            ballerConfigBuilder.setRewards(ballerConfig.getRewards());
            builder.addBallerConfig(ballerConfigBuilder);
        }

        for (List<WorldCupSkillConfig> skillConfigs : config.getSkillConfig().values()) {
            for (WorldCupSkillConfig skillConfig : skillConfigs) {
                WorldCupProto.WorldCupCollegeSkillConfigTemp.Builder skillConfigBuilder = WorldCupProto.WorldCupCollegeSkillConfigTemp.newBuilder();
                skillConfigBuilder.setType(skillConfig.getType());
                skillConfigBuilder.setLevel(skillConfig.getLevel());
                skillConfigBuilder.setIcon(skillConfig.getIcon());
                String toName = ServerLanguageMgr.getContent(skillConfig.getName(), language);
                skillConfigBuilder.setName(toName);
                String toDesc = ServerLanguageMgr.getContent(skillConfig.getDesc(), language);
                skillConfigBuilder.setDesc(toDesc);
                skillConfigBuilder.setCost(skillConfig.getCost());
                skillConfigBuilder.setRecruitInfo(skillConfig.getRecruitInfo());
                builder.addCollegeSkillConfig(skillConfigBuilder);
            }
        }
        return builder;
    }

    public static WorldCupProto.UserWorldCupData.Builder buildUserData(UserWorldCupData userData, List<UserWorldCupBallerData> ballers) {
        WorldCupProto.UserWorldCupData.Builder builder = WorldCupProto.UserWorldCupData.newBuilder();
        builder.setScore(userData.getScore());
        for (WorldCupSkill skill : userData.getSkillMap().values()) {
            WorldCupProto.WorldCupCollegeSkillTemp.Builder skillBuilder = WorldCupProto.WorldCupCollegeSkillTemp.newBuilder();
            skillBuilder.setType(skill.getType());
            skillBuilder.setLevel(skill.getLevel());
            skillBuilder.setExp(skill.getExp());
            skillBuilder.setRecruitAmount(userData.getRecruitAmount());
            builder.addSkillInfo(skillBuilder);
        }

        for (UserWorldCupBallerData baller : ballers) {
            WorldCupProto.WorldCupBallerTemp.Builder ballerBuilder = WorldCupProto.WorldCupBallerTemp.newBuilder();
            ballerBuilder.setBallerId(baller.getBallerId());
            ballerBuilder.setQuality(baller.getQuality());
            ballerBuilder.setAbility(baller.getAbility());
            ballerBuilder.setRecruitTime(baller.getRecruitTime());
            builder.addBallerInfo(ballerBuilder);
        }

        return builder;
    }

    public static WorldCupProto.WorldCupRecruitRespMsg.Builder buildRecruitMsg(Map<Integer, WorldCupSkill> copyUserSkill, Map<Integer, Integer> expMap,
                                                                               List<UserWorldCupBallerData> recruitBallers, String rewards) {
        WorldCupProto.WorldCupRecruitRespMsg.Builder builder = WorldCupProto.WorldCupRecruitRespMsg.newBuilder();
        builder.setRet(0);
        for (Map.Entry<Integer, Integer> expchange : expMap.entrySet()) {
            int type = expchange.getKey();
            int expAdd = expchange.getValue();
            WorldCupSkill skill = copyUserSkill.get(type);
            if (null == skill) {
                continue;
            }

            WorldCupProto.WorldCupCollegeSkillRecruitTemp.Builder skillBuilder = WorldCupProto.WorldCupCollegeSkillRecruitTemp.newBuilder();
            skillBuilder.setType(type);
            skillBuilder.setLevel(skill.getLevel());
            skillBuilder.setExp(skill.getExp());
            skillBuilder.setExpAdd(expAdd);
            builder.addSkills(skillBuilder);
        }

        for (UserWorldCupBallerData newBaller : recruitBallers) {
            WorldCupProto.WorldCupRecrutiBallerTemp.Builder ballerBulider = WorldCupProto.WorldCupRecrutiBallerTemp.newBuilder();
            ballerBulider.setBallerId(newBaller.getBallerId());
            ballerBulider.setQuaility(newBaller.getQuality());
            ballerBulider.setAbility(newBaller.getAbility());
            ballerBulider.setType(newBaller.getType());
            ballerBulider.setRecruitTime(newBaller.getRecruitTime());
            builder.addBallers(ballerBulider);
        }

        builder.setRewards(rewards);

        return builder;
    }
}
